define(['jquery', 'bootstrap', 'backend', 'form', 'table', 'tinymce'], function ($, undefined, Backend, Form, Table, tinymce) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'general/goods_for_score/index',
                    add_url: 'general/goods_for_score/add',
                    edit_url: 'general/goods_for_score/edit',
                    del_url: 'general/goods_for_score/del',
                    multi_url: 'general/goods_for_score/multi',
                    table: 'goods_for_score'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        { field: 'state', checkbox: true },
                        { field: 'id', title: "ID", searchable: false, search: false },
                        { field: 'category', title: "商品分类", searchable: false, },
                        { field: 'title', title: "商品标题" },
                        { field: 'price', title: "商品价格" },
                        { field: 'is_recommand', title: "推荐", formatter: Controller.api.formatter.yes_or_no, searchList: { "1": "是", "0": "否" } },
                        { field: 'weigh', title: "排序", searchable: false },
                        { field: 'cover', title: "商品封面图", searchable: false, formatter: Controller.api.formatter.thumb, search: false },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            // sortable: true,
                            width: 150
                        },
                        // { field: 'operate', title: "操作", table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                        {

                            field: 'buttons', title: '操作', table: table, operate: false,
                            buttons: [{
                                name: 'copy',
                                text: '复制',
                                title: '复制',
                                icon: 'fa fa-copy',
                                classname: 'btn btn-xs btn-info  btn-ajax',
                                //btn-xs：特别小按钮  btn-sm：小按钮  btn-lg：大按钮
                                //btn-info：一般信息（蓝色）  btn-danger：危险信息（红色） btn-default：默认样式（白色） btn-primary：首选项（深蓝） btn-success：成功（绿色） btn-warning：警告（黄色）
                                //btn-dialog：弹窗  btn-ajax：ajax请求  btn-addtabs：新选项卡
                                // classname: 'btn btn-xs btn-primary btn-dialog',
                                url: 'general/goods_for_score/copy_goods',
                                // extend: 'data-area=["80%","80%"]',
                                //成功返回参数
                                success: function (data, rest) {
                                    var opt = {
                                        url: '',
                                    };
                                    //刷新table表单
                                    table.bootstrapTable('refresh', opt);
                                    return true;
                                }
                            }],
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // $(table).data("operate-del", null);
                                $(table).data("operate-dragsort", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                        }
                    ]
                ],
                // commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                pageList: [10, 25, 50, 100, 'All']
            });


            // //弹出绑定到用户
            // $(".btn-bind-to-user").click(function () {
            //     var data_rows = $('#table').bootstrapTable('getAllSelections');
            //     console.log(data_rows);
            //     var ids_str = "";
            //     if (data_rows.length > 0) {
            //         var len = data_rows.length
            //         for (var i = 0; i < len; i++) {
            //             ids_str += data_rows[i]["id"] + ",";
            //         }
            //     } else {
            //         layer.msg("请选择要操作的行");
            //         return false;
            //     }

            //     Fast.api.open('general/goods/bind?gids=' + ids_str, "绑定用户", {
            //         //接收传过来的参数
            //         callback: function (value) {
            //             //在这里可以接收弹出层中使用`Fast.api.close(data)`进行回传数据
            //         },
            //         // 其他弹窗参数，如宽度、高度等
            //         area: ['80%', '80%']

            //     });
            // });

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["80%", "100%"]);

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit").data("area", ["80%", "100%"]);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var html = '';
                    if (value.indexOf("jpg") > -1 || value.indexOf("jpeg") > -1 || value.indexOf("png") > -1 || value.indexOf("gif") > -1) {
                        html = '<a href="' + row.cover + '" target="_blank"><img src="' + row.cover + '" alt="" style="max-height:60px;max-width:120px"></a>';
                    }
                    return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                },
                yes_or_no: function (value, row, index) {
                    var html = "";
                    if (value == "1") {
                        html = "是";
                    } else {
                        html = "否";
                    }
                    return html;
                },
            }
        },
    };
    return Controller;
});
